/**
 * 
 */
package com.wisdytech.linkdcs.equipment.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.plugins.Page;
import com.wisdytech.app.produceExecute.common.bean.ParamMq;
import com.wisdytech.common.bean.event.DcsParamChangeEvent;
import com.wisdytech.common.bean.event.MesParamChangeEvent;
import com.wisdytech.common.enums.ActionType;
import com.wisdytech.linkdcs.equipment.dao.*;
import com.wisdytech.linkdcs.equipment.model.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.thrift.service.IEquipmentCollectionInfoService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.EquipmentKeyUtils;
import com.wisdytech.common.utils.ImportExcelUtils;
import com.wisdytech.linkdcs.equipment.service.IEquipmentParamService;
import com.wisdytech.linkdcs.system.dao.ISysDictDao;
import com.wisdytech.linkdcs.system.model.SysDict;
import com.wisdytech.linkdcs.system.model.SysUser;
import com.wisdytech.linkdcs.tool.model.ToolConfig;

/**
 * <p>
 * ClassName: EquipmentParamServiceImpl
 * </p>
 * <p>
 * Description: 参数管理业务层
 * </p>
 * <p>
 * Author: kongming
 * </p>
 * <p>
 * Date: 2017-9-26
 * </p>
 */
@Service("equipmentParamService")
@Transactional
public class EquipmentParamServiceImpl extends BaseService<IEqCollectionParamDao, CollectionParam> implements IEquipmentParamService {
    /**
     * 处理设备注册事件推送
     */
    @Autowired
    ApplicationContext applicationContext;
	/**
     * 注入eqCollectionParamDao
     */
    @Resource
    private IEqCollectionParamDao eqCollectionParamDao;
    /**
     * 注入eqModelDao
     */
    @Resource
    private IEqModelDao eqModelDao;
    /**
     * 注入eqCollectionClientDao
     */
    @Resource
    private IEqCollectionClientDao eqCollectionClientDao;
    /**
     * 注入告警dao
     */
    @Resource
    private IEqEquipAlarmParamDao alarmParamDao;
    
    @Resource
    private IEqEquipmentDao equipmentDao;

    @Resource
    private IEquipmentCollectionInfoService equipmentCollectionInfoService;
    @Resource
    private ISysDictDao iSysDictDao;
    @Resource
    private IModelOperationDao iModelOperationDao;
    @Override
    public PageInfo<CollectionParam> listParamByFilter(String modelCode, String modelType, String paramName, Filter filter) {
        Map<String, Object> queryMap;
        queryMap = new HashMap<String, Object>();
        queryMap.put("modelCode", modelCode);
        queryMap.put("modelType", modelType);
        queryMap.put("paramName", paramName);
        List<CollectionParam> lists =
                super.beforeList(IEqCollectionParamDao.class, filter).selectParamPage(queryMap);
        PageInfo<CollectionParam> equipModelPage=new PageInfo<>(lists);
        return equipModelPage;
    }

    @Override
    public StandardResult addCollectionParam(CollectionParam param, String loginName) {
        EquipModel model = new EquipModel();
        model = eqModelDao.getEuipmentModelByEmModel(param.getModelCode());
        //获取工序
        String opCodes = null;
        List<ModelOperation> listOp = iModelOperationDao.listOperations(model.getModelCode());
        if (!listOp.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            for (ModelOperation operation : listOp) {
                sb.append(operation.getOpCode()).append(",");
            }
            //工序编码
            opCodes = sb.toString().substring(0, sb.toString().length() - 1);
        }
        //获取区域
        List<Equipment> listArea = equipmentDao.selectAreaByModelCode(param.getModelCode());
        String areaCodes = null;
        if (!listArea.isEmpty()) {
            if (listArea.size() == 1) {
                areaCodes = listArea.get(0).getAreaCode();
            } else {
                StringBuilder sbr = new StringBuilder();
                for (Equipment eq : listArea) {
                    sbr.append(eq.getAreaCode()).append(",");
                }
                //区域编码
                areaCodes = sbr.toString().substring(0, sbr.toString().length() - 1);
            }
        }
        CollectionClient client = new CollectionClient();
        client = eqCollectionClientDao.getCollectionClientByModelId(param.getModelCode());
        param.setCollectionClientId(client.getPkId());
        param.setOperationId(param.getOperationName());

        EntityWrapper<CollectionParam> ew1 = new EntityWrapper<>();
        List<CollectionParam> paramList1;
        paramList1 = eqCollectionParamDao.selectPage(new Page<>(1, 1),
                ew1.eq("del_flag", CommonConstants.DEL_FLAG)
                        .eq("COLLECTION_CLIENT_ID", param.getCollectionClientId()));
        if(!paramList1.isEmpty()){
            for(CollectionParam c : paramList1 ){
                if(c.getParamName().equals(param.getParamName())){
                    throw new ServiceException("同一型号下，参数名称不允许重复");
                }
            }
        }

        EntityWrapper<CollectionParam> ew = new EntityWrapper<>();
        List<CollectionParam> paramList;
        paramList = eqCollectionParamDao.selectPage(new Page<>(1, 1),
                ew.eq("PARAM_CODE", param.getParamCode())
                        .eq("del_flag", CommonConstants.DEL_FLAG)
                        .eq("COLLECTION_CLIENT_ID", param.getCollectionClientId()));

        if (!paramList.isEmpty()) {
            return StandardResult.getFailResult("参数编号已存在");
        }

        //通过规则获取参数的唯一id
        
        String pkId =
        		EquipmentKeyUtils.getParamKey(model.getModelCode(), param);
        CollectionParam uParam =eqCollectionParamDao.selectById(pkId);
        if(null!=uParam) {//唯一id存在，同一型号下参数地址已存在，不允许删除
        	throw new ServiceException("参数地址已存在！！！");
        }
        param.setPkId(pkId);

        param.setDelFlag(CommonConstants.DEL_FLAG);
        param.setCreateUser(loginName);
        param.setCreateDate(new Date());
        int paramCount = eqCollectionParamDao.insert(param);
        if (paramCount <=0) {
            throw new ServiceException("新增失败");
        }
        
        //推送生产类设备新增过程采集项或结果采集项
        if (model.getModelType().equals("1") || model.getModelType().equals("5")) {
            if (param.getParamType().equals("1") || param.getParamType().equals("2")) {
                ParamMq paramMq = sendProParam(param, opCodes, loginName);
                MesParamChangeEvent mesEvent = new MesParamChangeEvent(this, ActionType.ADD, paramMq);
                applicationContext.publishEvent(mesEvent);
            }
        }
        //推送环境类设备新增环境采集项
        if (model.getModelType().equals("2") && param.getParamType().equals("3")) {
            ParamMq paramMQ = sendEvnParam(param, areaCodes, loginName);
            MesParamChangeEvent mesEvent = new MesParamChangeEvent(this, ActionType.ADD, paramMQ);
            applicationContext.publishEvent(mesEvent);
        }

        //推送设备参数到dcs
//        处理推送
        DcsParamChangeEvent changeEvent = new DcsParamChangeEvent(this, ActionType.ADD, param, param.getCollectionClientId());
        applicationContext.publishEvent(changeEvent);
        //推送设备参数到dcs
//        equipmentCollectionInfoService.addByEquipmentParam(param.getPkId());

        return StandardResult.getSuccessResult();
    }

    private ParamMq sendEvnParam(CollectionParam param, String areaCodes, String loginName) {
        ParamMq paramMQ = new ParamMq();
        paramMQ.setId(param.getPkId());
        paramMQ.setAreaCode(areaCodes);
        paramMQ.setFieldName(param.getParamCode());
        paramMQ.setParamName(param.getParamName());
        paramMQ.setParamType(param.getParamType());
        paramMQ.setUnit(param.getProcessRange());
        paramMQ.setCreateUser(param.getCreateUser());
        paramMQ.setCreateDate(param.getCreateDate());
        paramMQ.setUpdateUser(param.getUpdateUser());
        paramMQ.setUpdateDate(param.getUpdateDate());
        param.setRemark(param.getRemark());
        return paramMQ;
    }

    private ParamMq sendProParam(CollectionParam param, String opCodes, String loginName) {
        ParamMq paramMq = new ParamMq();
        paramMq.setId(param.getPkId());
        paramMq.setOpCode(opCodes);
        paramMq.setFieldName(param.getParamCode());
        paramMq.setParamName(param.getParamName());
        paramMq.setParamType(param.getParamType());
        paramMq.setUnit(param.getProcessRange());
        paramMq.setCreateUser(param.getCreateUser());
        paramMq.setCreateDate(param.getCreateDate());
        paramMq.setUpdateUser(param.getUpdateUser());
        paramMq.setUpdateDate(param.getUpdateDate());
        param.setRemark(param.getRemark());
        return paramMq;
    }
    @Override
    public StandardResult delCollectionParam(String id) {
        //参数删除推送事件对象
        List<CollectionParam> paramChangeParams = new ArrayList<>();

        List<ParamMq> paramMqs = new ArrayList<>();
        List<String> idList = new ArrayList<String>();
        String[] ids = id.split(",");
        for (int i = 0; i < ids.length; i++) {
            idList.add(ids[i]);
        }
       
        for (String pkId : ids) {
            CollectionParam param = eqCollectionParamDao.selectById(pkId);
            EquipModel model = eqModelDao.getEuipmentModelByParamId(pkId);
            //推送生产类设备新增过程采集项或结果采集项
            if (model.getModelType().equals("1")) {
                if (param.getParamType().equals("1") || param.getParamType().equals("2")) {
                    ParamMq paramMq = new ParamMq();
                    paramMq.setId(param.getPkId());
                    paramMq.setFieldName(param.getParamCode());
                    paramMq.setParamType(param.getParamType());
                    paramMqs.add(paramMq);
                }
            }
            //推送环境类设备新增环境采集项
            if (model.getModelType().equals("2") && param.getParamType().equals("3")) {
                ParamMq paramMq = new ParamMq();
                paramMq.setId(param.getPkId());
                paramMq.setFieldName(param.getParamCode());
                paramMq.setParamType(param.getParamType());
                paramMqs.add(paramMq);
            }
            paramChangeParams.add(param);
            EntityWrapper<EquipAlarmParam> ew = new EntityWrapper<>();
            List<EquipAlarmParam> alarmParamList = alarmParamDao.selectList(ew.eq("PARAM_ID", param.getPkId()).eq("DEL_FLAG", 0));
            if (!alarmParamList.isEmpty()) {
                return StandardResult.getFailResult(CommonConstants.EQUIPMENT_ALARM_PARAM);
            }
            //0--代表参数没有可视化管理设置--可以删除
            if ("0".equals(param.getShowLevel())) {
                int count = eqCollectionParamDao.deleteById(pkId);
                if (count <= 0){
                        throw new ServiceException("删除失败");
                }
            }else {
                return StandardResult.getFailResult(CommonConstants.PARAM_SHOWLEVEL);
            }
        }
        
        //推送设备参数到dcs
        //顺序推送删除参数
        for (CollectionParam paramChange : paramChangeParams) {
            DcsParamChangeEvent changeEvent = new DcsParamChangeEvent(
                    this, ActionType.ADD, paramChange, paramChange.getCollectionClientId());
            applicationContext.publishEvent(changeEvent);
        }
        //
        for (ParamMq paramMq : paramMqs) {
            MesParamChangeEvent mesEvent = new MesParamChangeEvent(this, ActionType.DEL, paramMq);
            applicationContext.publishEvent(mesEvent);
        }
        return StandardResult.getSuccessResult();
    }

    @Override
    public StandardResult updateCollectionParam(CollectionParam param, String loginName) {
        EquipModel model = eqModelDao.getEuipmentModelByEmModel(param.getModelCode());
        CollectionClient client = new CollectionClient();
        client = eqCollectionClientDao.getCollectionClientByModelId(model.getModelCode());

        //获取工序
        String opCodes = null;
        List<ModelOperation> listOp = iModelOperationDao.listOperations(model.getModelCode());
        if (!listOp.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            for (ModelOperation operation : listOp) {
                sb.append(operation.getOpCode()).append(",");
            }
            //工序编码
            opCodes = sb.toString().substring(0, sb.toString().length() - 1);
        }

        //获取区域
        List<Equipment> listArea = equipmentDao.selectAreaByModelCode(param.getModelCode());
        String areaCodes = null;
        if (!listArea.isEmpty()) {
            if (listArea.size() == 1) {
                areaCodes = listArea.get(0).getAreaCode();
            } else {
                StringBuilder sbr = new StringBuilder();
                for (Equipment eq : listArea) {
                    sbr.append(eq.getAreaCode()).append(",");
                }
                //区域编码
                areaCodes = sbr.toString().substring(0, sbr.toString().length() - 1);
            }
        }
        
        EntityWrapper<CollectionParam> ew = new EntityWrapper<>();
        List<CollectionParam> paramList;
        paramList = eqCollectionParamDao.selectPage(new Page<CollectionParam>(1, 1),
                ew.eq("PARAM_CODE", param.getParamCode())
                        .ne("pk_id", param.getPkId())
                        .eq("COLLECTION_CLIENT_ID",client.getPkId())
                        .eq("del_flag", CommonConstants.DEL_FLAG));

        if (!paramList.isEmpty()) {
            return StandardResult.getFailResult("参数编号已存在");
        }
        CollectionParam p = eqCollectionParamDao.selectById(param.getPkId());
        param.setCreateUser(p.getCreateUser());
        param.setCreateDate(p.getCreateDate());
        param.setCollectionClientId(client.getPkId());
        param.setOperationId(param.getOperationName());
        param.setUpdateUser(loginName);
        param.setUpdateDate(new Date());
        int count = eqCollectionParamDao.updateById(param);
        if (count <= 0) {
            throw new ServiceException("修改失败");
        }
        
        //推送生产类设备新增过程采集项或结果采集项
        if (model.getModelType().equals("1")) {
            if (param.getParamType().equals("1") || param.getParamType().equals("2")) {
                ParamMq paramMq = sendProParam(param, opCodes, loginName);
                MesParamChangeEvent mesEvent = new MesParamChangeEvent(this, ActionType.UPDATE, paramMq);
                applicationContext.publishEvent(mesEvent);
            }
        }
        //推送环境类设备新增环境采集项
        if (model.getModelType().equals("2") && param.getParamType().equals("3")) {
            ParamMq paramMQ = sendEvnParam(param, areaCodes, loginName);
            MesParamChangeEvent mesEvent = new MesParamChangeEvent(this, ActionType.UPDATE, paramMQ);
            applicationContext.publishEvent(mesEvent);
        }

        //推送设备参数到dcs
        //查找最新的参数记录
        CollectionParam nParam = eqCollectionParamDao.selectById(param.getPkId());
        DcsParamChangeEvent changeEvent = new DcsParamChangeEvent(this, ActionType.UPDATE, nParam, nParam.getCollectionClientId());
        applicationContext.publishEvent(changeEvent);
        
        
        return StandardResult.getSuccessResult();
    }

    @Override
    public CollectionParam getEuipmentParamById(String pkId) {
        return eqCollectionParamDao.getEuipmentParamById(pkId);
    }

    @Override
    public List<CollectionParam> listModelParams(String modelId) {
        // TODO Auto-generated method stub
        return eqCollectionParamDao.selectParamByModelId(modelId);
        
    }

    @Override
    public List<CollectionParam> listModelParamsByModelCode(String modelCode) {

        return eqCollectionParamDao.selectParamByModelCode(modelCode);

    }


    @Override
    public List<CollectionParam> listEqParams(String eqCode) {
        
        return eqCollectionParamDao.selectParamByEqCode(eqCode, "", "");
    }

	@Override
    public PageInfo<CollectionParam> listParams(String eqCode, String search, Filter filter) {
        List<CollectionParam>  params =this.beforeList(IEqCollectionParamDao.class, filter).selectParamByEqCode(eqCode,search,"5");
        return new PageInfo<CollectionParam>(params);
    }

    @Override
	public List<CollectionParam> listModelParams(String modelId,
			String paramName, int page, int size) {
		Filter filter =new Filter();
		filter.setNum(page);
		filter.setSize(size);
		
		List<CollectionParam>  params =beforeList(IEqCollectionParamDao.class, filter).
			selectParamByModelIdAndName(modelId, paramName);
		
		
		return params;
	}

	@Override
	public List<SysDict> getProcessRange(String processRange) {
		EntityWrapper<SysDict> ew = new EntityWrapper<>();
		ew.eq("PARENT_KEY", "MATERIAL_UNIT");
		if(processRange!=null && processRange !="") {
			ew.like("DICT_VALUE", processRange);
		}
		return iSysDictDao.selectList(ew);
	}

	@Override
	public void batchImportParam(MultipartFile file, SysUser operator) {
        if (file == null) {
            throw new ServiceException("文件为空");
        }
        if (operator ==null) {
            throw new ServiceException("操作人不能为空");
        }
        int errorRow=0;
        try {
            String fileName = file.getOriginalFilename();
            InputStream in = file.getInputStream();
            List<List<Object>> data = ImportExcelUtils.getDataListByExcel(in, fileName);
            if (data == null || data.size() == 0) {
                throw new ServiceException("表格数据为空");
            }

            //i 从1开始，去除表头数据，根据自己的模板而定
            for (int i = 1; i < data.size(); i++) {
                errorRow = i;
                CollectionParam param = new CollectionParam();
                param.setPkId(BaseUtil.getUUID());
                param.setCreateDate(new Date());
                param.setCreateUser(operator.getUsername());
                param.setDelFlag(CommonConstants.DEL_FLAG);
                if (data.get(i).get(0) == null||StringUtils.isBlank(data.get(i).get(0).toString())) {
                    throw new ServiceException("第"+i+"行第1列设备型号数据不能为空");
                }
                String modelCode = data.get(i).get(0).toString();
                if(!checkModelCode(data.get(i).get(0).toString())) {
                    throw new ServiceException("第"+i+"行第1列设备型号数据在数据库中不存在");
                }
                EquipModel model = new EquipModel();
                model = eqModelDao.getEuipmentModelByEmModel(modelCode);
                CollectionClient client = new CollectionClient();
                client = eqCollectionClientDao.getCollectionClientByModelId(model.getModelCode());
                param.setCollectionClientId(client.getPkId());
                if (data.get(i).get(1) == null||StringUtils.isBlank(data.get(i).get(1).toString())) {
                    throw new ServiceException("第"+i+"行第2列参数变量不能为空");
                }
                param.setParamName(data.get(i).get(1).toString());
                if (data.get(i).get(2) == null||StringUtils.isBlank(data.get(i).get(2).toString())) {
                    throw new ServiceException("第"+i+"行第3列采集方式不能为空");
                }
                if(!checkCollectionType(data.get(i).get(2).toString())) {
                    throw new ServiceException("第"+i+"行第3列采集方式数据有误");
                }
                param.setCollectionType(data.get(i).get(2).toString());
                //判断采集方式
                String type = data.get(i).get(2).toString();
                if(type.equals("1")) {
                    if (data.get(i).get(3) == null||StringUtils.isBlank(data.get(i).get(3).toString())) {
                        throw new ServiceException("第"+i +"行第4列PLC地址不能为空");
                    }
                	param.setPlcAddr(data.get(i).get(3).toString());
                }
                if(type.equals("2")) {
                    if (data.get(i).get(4) == null||StringUtils.isBlank(data.get(i).get(4).toString())) {
                        throw new ServiceException("第"+i +"行第5列文件名称不能为空");
                    }
                    if (data.get(i).get(5) == null||StringUtils.isBlank(data.get(i).get(5).toString())) {
                        throw new ServiceException("第"+i +"行第6列文件类型不能为空");
                    }
                    if (data.get(i).get(6) == null||StringUtils.isBlank(data.get(i).get(6).toString())) {
                        throw new ServiceException("第"+i +"行第7列列号不能为空");
                    }
                    if (data.get(i).get(7) == null||StringUtils.isBlank(data.get(i).get(7).toString())) {
                        throw new ServiceException("第"+i +"行第8列是否忽略首行不能为空");
                    }
                    param.setFileName(data.get(i).get(4).toString());
                    param.setFileType(data.get(i).get(5).toString());
                    param.setFileColumn(data.get(i).get(6).toString());
                    param.setFileIgnoreFirst(data.get(i).get(7).toString());
                }
                if(type.equals("3")) {
                    if (data.get(i).get(8) == null||StringUtils.isBlank(data.get(i).get(8).toString())) {
                        throw new ServiceException("第"+i +"行第9列字段地址不能为空");
                    }
                    param.setDatabaseParamAddr(data.get(i).get(8).toString());
                }
                if(type.equals("4")) {
                    if (data.get(i).get(9) == null||StringUtils.isBlank(data.get(i).get(9).toString())) {
                        throw new ServiceException("第"+i +"行第10列上位机数据定义不能为空");
                    }
                    if (data.get(i).get(10) == null||StringUtils.isBlank(data.get(i).get(10).toString())) {
                        throw new ServiceException("第"+i +"行第11列上位机参数名称不能为空");
                    }
                    if (data.get(i).get(11) == null||StringUtils.isBlank(data.get(i).get(11).toString())) {
                        throw new ServiceException("第"+i +"行第12列上位机参数字段不能为空");
                    }
                    param.setUpperDefinition(data.get(i).get(9).toString());
                    param.setUpperParamName(data.get(i).get(10).toString());
                    param.setUpperParamField(data.get(i).get(11).toString());
                }
                if (data.get(i).get(12) == null||StringUtils.isBlank(data.get(i).get(12).toString())) {
                    throw new ServiceException("第"+i +"行第13列工艺单位不能为空");
                }
                param.setProcessRange(data.get(i).get(12).toString());
                if (data.get(i).get(13) == null||StringUtils.isBlank(data.get(i).get(13).toString())) {
                    throw new ServiceException("第"+i +"行第14列缩放比例不能为空");
                }
                param.setEnlargeNum( Double.parseDouble(data.get(i).get(13).toString()));
                if (data.get(i).get(14) == null||StringUtils.isBlank(data.get(i).get(14).toString())) {
                    throw new ServiceException("第"+i +"行第15列数据类型不能为空");
                }
                param.setDataType(data.get(i).get(14).toString());
                if (data.get(i).get(15) == null||StringUtils.isBlank(data.get(i).get(15).toString())) {
                    throw new ServiceException("第"+i +"行第16列读写属性不能为空");
                }
                param.setRwAttr(Integer.parseInt( data.get(i).get(15).toString()));
                if (data.get(i).get(16) == null||StringUtils.isBlank(data.get(i).get(16).toString())) {
                    throw new ServiceException("第"+i +"行第17列采集项类型不能为空");
                }
                param.setParamType(data.get(i).get(16).toString());
                if (data.get(i).get(17) == null||StringUtils.isBlank(data.get(i).get(17).toString())) {
                    throw new ServiceException("第"+i +"行第18列是否自动告警不能为空");
                }
                param.setIsAutoAlarm(data.get(i).get(17).toString());
                param.setAutoAlarmName(data.get(i).get(18).toString());
                if (data.get(i).get(19) == null||StringUtils.isBlank(data.get(i).get(19).toString())) {
                    throw new ServiceException("第"+i +"行第20列SPC标准值为空");
                }
                param.setSpcValue(new BigDecimal(data.get(i).get(19).toString()));
                if (data.get(i).get(20) == null||StringUtils.isBlank(data.get(i).get(20).toString())) {
                    throw new ServiceException("第"+i +"行第21列参数编号为空");
                }
                EntityWrapper<CollectionParam> ew = new EntityWrapper<>();
                ew.eq("PARAM_CODE", data.get(i).get(20).toString());
                ew.eq("DEL_FLAG", CommonConstants.DEL_FLAG);
                int count = eqCollectionParamDao.selectCount(ew);
                if(count>0) {
                	throw new ServiceException("参数编号已存在");
                }
                param.setParamCode(data.get(i).get(20).toString());
                param.setAlarmCode(data.get(i).get(21).toString());
                param.setDataLength(Integer.parseInt(data.get(i).get(22).toString()));
                param.setDecimalPos(Integer.parseInt(data.get(i).get(23).toString()));
                param.setParamField(data.get(i).get(24).toString());
                param.insert();
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServiceException("上传文件出错");
        } catch (IndexOutOfBoundsException e2) {
            e2.printStackTrace();
            throw new ServiceException("表格中第"+errorRow+"存在空数据");
        }
    }
	
    private boolean checkCollectionType(String string) {
		String [] types = {"1","2","3","4"};
		return Arrays.asList(types).contains(string);
	}

	private Boolean checkModelCode(String modelCode) {
        if (StringUtils.isBlank(modelCode)) {
            return false;
        }
        EntityWrapper<EquipModel> ew = new EntityWrapper<>();
        ew.eq("model_code",modelCode).eq("del_flag",CommonConstants.DEL_FLAG);
        return eqModelDao.selectCount(ew)>0;
    }

	@Override
	public List<SysDict> getDataType(String search) {
		EntityWrapper<SysDict> ew  = new EntityWrapper<>();
		ew.eq("PARENT_KEY", "DATA_TYPE");
		if(StringUtils.isNotEmpty(search)) {
			ew.like("DICT_VALUE", search);
		}
		return iSysDictDao.selectList(ew);
	}

	
}
